package com.google.coffeeshop.dao.impl;

import java.util.List;

import com.google.coffeeshop.dao.ProductDAO;
import com.google.coffeeshop.entity.ProductDTO;
import com.googlecode.genericdao.search.Search;


public class ProductDAOImpl extends AbstractBaseDAOImpl<ProductDTO, Long> implements ProductDAO {
	public ProductDTO findActiveById(Long id) {
		Search search = new Search(ProductDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(1);
		search.addFilterEqual("id", id);
		search.addFilterEqual("active",Boolean.TRUE);
		return this.searchUnique(search);
	}
	
	public List<ProductDTO> findAllActive() {
		Search search = new Search(ProductDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(Integer.MAX_VALUE);
		search.addFilterEqual("active", Boolean.TRUE);
		List<ProductDTO> products = this.search(search);
		if (null == products || products.isEmpty()) {
			return null;
		}
		return products;
	}

	@Override
	public List<ProductDTO> findActiveByGroupId(long groupId) {
		Search search = new Search(ProductDTO.class);
		search.setFirstResult(0);
		search.setMaxResults(Integer.MAX_VALUE);
		search.addFilterEqual("active", Boolean.TRUE);
		search.addFilterEqual("group.id", groupId);
		List<ProductDTO> products = this.search(search);
		if (null == products || products.isEmpty()) {
			return null;
		}
		return products;
	}
}
